import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/Login.vue')
    },
    {
      path: '/player',
      name: 'music',
      component: () => import("../views/3DPhoto/GeoMetryBox.vue")
    },
    {
      path: '/',
      name: 'home',
      component: HomeView,
      redirect: '/about',
      children: [
        {
          path: '/about',
          name: 'about',
          meta: {
            title: '首页'
          },
          component: () => import('../views/About.vue')
        },
        {
          path: "/realtime",
          name: "realTime",
          meta: {
            title: "实时数据"
          },
          component: () => import("../views/data/RealTime.vue")
        },
        {
          path: '/device',
          name: 'device',
          meta: {
            title: '设备管理'
          },
          component: () => import('../views/manage/Device.vue')
        },
        {
          path: '/projects',
          name: 'projects',
          meta: {
            title: '项目'
          },
          component: () => import('../views/Projects.vue')
        },
        {
          path: '/projects/box',
          name: 'projectsBox',
          meta: {
            title: "实时数据"
          },
          component: () => import('../views/Box.vue')
        },
        {
          path: '/projects/box',
          name: 'projectsBox',
          meta: {
            title: "项目详情"
          },
          component: () => import('../views/Box.vue')
        },
        {
          path: '/projects/box/variant',
          name: 'variant',
          meta: {
            title: "项目设备"
          },
          component: () => import('../views/Variant.vue')
        },
        {
          path: '/projects/box/chart',
          name: 'chart',
          meta: {
            title: "折线图"
          },
          component: () => import('../views/Chart.vue')
        },
        {
          path: '/screen',
          name: 'screen',
          meta: {
            title: "投屏界面"
          },
          component: () => import('../views/Screen.vue')
        },
      ]
    },
    {
      path: '/m/home',
      name: 'mhome',
      component: ()=>import("@/views/mobile/Index.vue")
    },
    {
      path: '/m/sensor',
      name: 'msensor',
      component: ()=>import("@/views/mobile/Sensor.vue")
    },    
    {
      path: '/m/data',
      name: 'mdata',
      component: ()=>import("@/views/mobile/SensorDetail.vue")
    },
    {
      path: '/m/chart',
      name: 'mchart',
      component: ()=>import("@/views/mobile/Chart.vue")
    },
    {
      path: '/m/settings',
      name: 'msettings',
      component: ()=>import("@/views/mobile/Settings.vue")
    },
  ]
})
// 挂载路由导航守卫：to表示将要访问的路径，from表示从哪里来，next是下一个要做的操作
router.beforeEach((to, from, next) => {

  // 修改页面 title
  if (to.meta.title) {
    document.title = 'IoT - ' + to.meta.title
  }
  // 放行登录页面
  if (to.path === '/login') {
    return next()
  }
  // 获取token
  const token = sessionStorage.getItem('token')
  if (!token) {
    return next('/login')
  } else {
    to.matched.forEach(record => {
      record.meta.fullPath = to.fullPath; // 将当前路由的 fullPath 存储在 meta 中
    });
    next()
  }
})
export default router
